package cn.vc.auth.controller;

import cn.vc.auth.entity.po.VUser;
import cn.vc.auth.util.SecurityUtil;
import cn.vc.basicmodule.entity.Result;
import cn.vc.auth.entity.po.Relation;
import cn.vc.auth.service.RelationService;
import cn.vc.auth.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 关系接口
 */
@Slf4j
@RestController
@RequestMapping("/relation")
//@PreAuthorize("hasAuthority('ordinary')")
public class RelationController {
    @Resource
    private RelationService relationService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UserService userService;



    /**
     * 关注用户
     */
    @PostMapping
    public Result<Relation> attention(@RequestParam("id") Long id) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        int bo = relationService.insert(myUser.getId(), id);
        String msg = "";
        switch (bo) {
            case 1:
                msg="关注成功！";
                userService.attention(id);
                break;
            case 2:
                msg="关注成功，恭喜成为好友关系！";
                userService.attention(id);
                break;
            case 0:
                msg="你已经关注了哦~";
                break;
            case -1:
                msg="关注失败！";
        }
        return new Result<>(bo, null, msg);
    }


    /**
     * 取关用户
     * @param id
     */
    @DeleteMapping
    public Result<Relation> notAttention(@RequestParam("id") Long id) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        boolean bo = relationService.delete(myUser.getId(), id);
        if (bo) {
            userService.noAttention(id);
        }
        return new Result<>(bo);
    }




    /**
     * 查询我的全部好友
     */
    @GetMapping("/00")
    public Result<List<Relation>> selectMyFriend() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        // 查询我的关注列表
        List<Relation> relationList = relationService.selectOnFriend(myUser.getId());
        return new Result<>(1, relationList);
    }
    /**
     * 查询我的全部好友
     */
    @GetMapping("/01")
    public Result<List<VUser>> selectMyFriendOnVUser() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        // 查询我的关注列表
        List<Relation> relationList = relationService.selectOnFriend(myUser.getId());
        List<Long> ids = relationList.stream().map(relation -> {
            if (relation.getUserId1().equals(myUser.getId())) {
                return relation.getUserId2();
            } else {
                return relation.getUserId1();
            }
        }).collect(Collectors.toList());
        List<VUser> vUserList = userService.selectByIds(ids);
        return new Result<>(1, vUserList);
    }
    /**
     * 查询我的全部关注
     */
    @GetMapping("/10")
    public Result<List<Relation>> selectMyAttention() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        // 查询我的关注列表
        List<Relation> relationList = relationService.selectByUserId1(myUser.getId());
        return new Result<>(1, relationList);
    }
    /**
     * 查询我的全部关注
     */
    @GetMapping("/11")
    public Result<List<VUser>> selectMyAttentionOnVUser() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        // 查询我的关注列表
        List<Relation> relationList = relationService.selectByUserId1(myUser.getId());
        List<Long> ids = relationList.stream()
                .map(Relation::getUserId2)
                .collect(Collectors.toList());
        List<VUser> vUserList = userService.selectByIds(ids);
        return new Result<>(1, vUserList);
    }
    /**
     * 查询我的全部粉丝
     */
    @GetMapping("/20")
    public Result<List<Relation>> selectMyFans() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        // 查询我的粉丝列表
        List<Relation> relationList = relationService.selectByUserId2(myUser.getId());
        return new Result<>(1, relationList);
    }
    /**
     * 查询我的全部粉丝
     */
    @GetMapping("/21")
    public Result<List<VUser>> selectMyFansOnVUser() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        // 查询我的粉丝列表
        List<Relation> relationList = relationService.selectByUserId2(myUser.getId());
        List<Long> ids = relationList.stream()
                .map(Relation::getUserId1)
                .collect(Collectors.toList());
        List<VUser> vUserList = userService.selectByIds(ids);
        return new Result<>(1, vUserList);
    }
    /**
     * 查询我是否关注某个用户
     * @param userId2
     */
    @GetMapping("/1/{id}")
    public Result<?> selectIsAttention(@PathVariable(value = "id") Long userId2) {
        VUser vUser = SecurityUtil.getUser();
        if (vUser==null) return Result.NOT_LOGGED_IN();
        log.info("查询是否关注用户: { userId1 = " + vUser.getId() + ", userId2 = " + userId2 + " }");
        // 搜索关注
        Relation relation =  relationService.selectIsAttention(vUser.getId(), userId2);
        return new Result<>(relation!=null?1:0, relation);
    }
    /**
     * 查询我是否被某个用户关注
     * @param userId2
     */
    @GetMapping("/2/{id}")
    public Result<?> selectIsBeAttention(@PathVariable(value = "id") Long userId2) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        log.info("查询是否被用户关注: { userId1 = " + myUser.getId() + ", userId2 = " + myUser.getId() + " }");
        // 搜索关注
        Relation relation =  relationService.selectIsAttention(userId2, myUser.getId());
        return new Result<>(relation!=null?1:0, relation);
    }
}
